package com.pakazaka.roguelike.ui;

import com.pakazaka.simplecrawler.Core;
import static com.pakazaka.simplecrawler.Core.getInstance;
import com.pakazaka.simplecrawler.SimpleCrawler;
import com.pakazaka.simplecrawler.Window;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

/**
 *
 * @author Remi
 */
public class StatsWindow extends Window implements MouseListener, MouseMotionListener {

    public StatsWindow(int width, int height, int x, int y, int priority, String caption) {
        super(width, height, x, y, priority, caption);
    }

    @Override
    public void paint(Graphics g, Object obj) {
        paintBack(g);

    }

    protected void paintBack(Graphics g) {
        g.setColor(Color.black);
        g.fillRect(x, y, width, height);

        g.setColor(Color.white);
        g.drawRect(x, y, width, height);
        g.drawString(caption + " " + priority, x + (width / 2 - 32), y + 16);
    }

    @Override
    public int getWidth() {
        return width;
    }

    @Override
    public int getHeight() {
        return height;
    }

    @Override
    public int getX() {
        return x;
    }

    @Override
    public int getY() {
        return y;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (isOnWindow(e)) {
            System.out.println("Pressed on: " + caption);
        }

    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        dragging = false;
        dragged = false;
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        if (!dragging && !dragged) {
            ofx = e.getX() - x;
            ofy = e.getY() - y;
            if (isOnWindow(e)) {
                dragging = true;
                dragged = true;
                changePriority();
            }

        }
        if (dragging) {
            this.x = e.getX() - ofx;
            this.y = e.getY() - ofy;
        }
        getInstance().repaint();
    }

    @Override
    public void mouseMoved(MouseEvent e) {
    }

    private boolean isOnWindow(MouseEvent e) {
        return (e.getX() > getX()) && (e.getX() < (getX() + getWidth())) && (e.getY() > getY()) && (e.getY() < (getY() + getHeight()));
    }

    public void changePriority() {
        Window temp = null;
        Core core = getInstance();
        for (int i = 0; i < core.getWindows().size(); i++) {
            if (core.getWindows().get(i).getPriority() == i) {
                temp = core.getWindows().get(i);
            }
        }
    }
}
